package com.lengmou.ctrl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lengmou.entity.Users;
import com.lengmou.service.UsersService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/iuu")
public class AdminController {
    @Resource
    private UsersService usersService;
    @RequestMapping(value = "/getUsers",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getUsers(@RequestBody Map<String,Integer> map){
        PageHelper.startPage(map.get("pageNum"),map.get("pageSize"));
        List<Users> usersAll = usersService.getUsersAll();
        JSONObject jsonObject = new JSONObject();
        PageInfo<Users> pageInfo = new PageInfo<>(usersAll);
        jsonObject.put("code",0);
        jsonObject.put("count",pageInfo.getNavigatepageNums().length*map.get("pageSize"));
        jsonObject.put("mes","nice");
        jsonObject.put("data",usersAll);

        return jsonObject.toJSONString();
    }
    @RequestMapping(value = "/updataUsers",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String updataUsers(Users users){
        JSONObject jsonObject = new JSONObject();
        Users userById = usersService.getUserById(users.getId());
        Users userByName = usersService.getUserByName(users.getUsername());
        if(users.getMoney()>=0){
            if(userById.getId()==userByName.getId()){
                int i = usersService.updateUser(users);
                jsonObject.put("state",i);
            }else{
                jsonObject.put("state",-3);
            }
        }else{
            jsonObject.put("state",-2);
        }

        return jsonObject.toJSONString();
    }
    @RequestMapping(value = "/setUsersState",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setUsersState(int id){
        JSONObject json = new JSONObject();
        Users userById = usersService.getUserById(id);
        if(userById!=null){
            if(userById.getState()==0){
                userById.setState(1);
                int i = usersService.updateUser(userById);
                json.put("state",i);
            }else{
                userById.setState(0);
                int i = usersService.updateUser(userById);
                json.put("state",i);
            }
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value = "/setUsersIdentity",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setUsersIdentity(int id){
        JSONObject json = new JSONObject();
        Users userById = usersService.getUserById(id);
        if(userById!=null){
            if(userById.getIdentity()==0){
                userById.setIdentity(1);
                json.put("state",usersService.updateUser(userById));

            }else{
                userById.setIdentity(0);
                json.put("state",usersService.updateUser(userById));
            }
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value = "/searchUsersName",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String searchUsersName(@RequestBody Map<String,String> map){
        JSONObject json = new JSONObject();
        Users userByName = usersService.getUserByName(map.get("username"));
        List<Users> usersList = new ArrayList<>();
        usersList.add(userByName);
        json.put("code",0);
        json.put("mes","nice");
        json.put("data",usersList);
        return json.toString();
    }
    @RequestMapping(value = "/addUsers",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String addUsers(Users user){
        JSONObject json = new JSONObject();
        int reg = usersService.reg(user);
        json.put("state",reg);
        return json.toString();
    }



    @RequestMapping(value = "/jsonUsers",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String jsonUsers(){
        JSONObject json = new JSONObject();
        List<Users> usersAll = usersService.getUsersAll();
        json.put("users",usersAll);
        return json.toString();
    }
    @RequestMapping(value = "/SttingState1",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String sttingState1(@RequestBody List<Users> usersList){
        JSONObject json = new JSONObject();
        if(usersList!=null&&usersList.size()>0){
            int i =0;
            for (Users u:usersList) {
                u.setState(1);
               i += usersService.setUsersState(u);
            }
            json.put("state",i);
        }else{
//            参数错误
            json.put("state",-1);
        }
        return json.toString();
    }


    @RequestMapping(value = "/SttingState0",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String sttingState0(@RequestBody List<Users> usersList){
        JSONObject json = new JSONObject();
        if(usersList!=null&&usersList.size()>0){
            int i =0;
            for (Users u:usersList) {
                u.setState(0);
                i += usersService.setUsersState(u);
            }
            json.put("state",i);
        }else{
//            参数错误
            json.put("state",-1);
        }
        return json.toString();
    }





}
